
*** Table 2 Simulated impacts : direct or indirect energy intensities



* COLUMNS ARE: TAX ONLY ; TAX + TRADE-BASED SUBSIDY ; TAX + NET TRADE-BASED SUBSIDY  ; TAX + CONSUMPTION BASED SUBSIDY (5 COLUMNS INCL COLUMN DEFINING VARIABLE TO BE SUMMARIZED) 

local filename "$filename"
file open Table using "$tables/`filename'.tex", write replace


* table head
file write Table "\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} p{1.6in} c c c c c }" _n
file write Table "\toprule " _n
file write Table "          &       Carbon Tax   & \multicolumn{3}{c}{Tax and Subsidy}  \\   \cline{3-5} " _n
file write Table " 	" _n
file write Table "          &    (1)  & (2)    & (3)     &    (4) \\ " _n
file write Table " \midrule " _n


* TOP PANEL OF THE TABLE REPORTS PC CHANGE IN PRODUCTION, IMPORTS, EXPORTS, NET IMPORTS. REPORT MEDIAN (5 PC 95 PC)

* statisitcs:

file write Table "\multicolumn{5}{c}{\textbf{A: Impacts on Domestic Shipments, Imports, Exports (\%)}} \\[2ex] "_n
*file write Table "\midrule " _n


foreach yvar in "prod"  "exp" "imp" {

    * (1) TAX
		
        * extract variable label
	    local label: variable label pc_`yvar'_t
		
		* stats
	    quietly sum pc_`yvar'_t, detail
		
		local p5_tax  = r(p5)
		local p50_tax = r(p50)
		local p95_tax = r(p95)
		

	* (2) TAX+subsidy
    foreach sub in $caselist  {
	
		* stats
	    quietly sum pc_`yvar'_`sub', detail
		
		local p5_`sub'  = r(p5)
		local p50_`sub' = r(p50)
		local p95_`sub' = r(p95)

	}
	
	* (3) write one line
	file write Table "`label' "	
	foreach sub in $caselist {
		file write Table " & $" _tab %7.1f (`p50_`sub'')  "$" 
	}
	file write Table " \\ " _n
	foreach sub in  $caselist {
		file write Table " &" _tab  "$[" %7.1f (`p5_`sub'') "," %7.1f (`p95_`sub'') "]$" 
	}
	file write Table " \\[0.5em]" _n
	
}
*


* Bottom PANEL REPORTS ABATEMENT, LEAKAGE_T, LEAKAGE_C, NET ABATEMENT PC _T NET ABATEMENT PC _C
file write Table "\midrule " _n
file write Table "\multicolumn{5}{c}{\textbf{B: Annual Emission Abatement and Emissions Leakage (\%)}} \\[2ex] "_n


* statisitcs:
foreach var in "abaterate"  "netabrate"  "leakagerate"  "tax_rev_agg"  "tr_agg"  "num_industries" {

    * (1) TAX
		
        * extract variable label
	    local label: variable label `var'_t
		
		* stats: trade 
	    quietly sum `var'_t, detail
		
		local p5_tax  = r(p5)
		local p50_tax = r(p50)
		local p95_tax = r(p95)
		

	* (2) TAX+subsidy
    foreach sub in $caselist  {
	    
		* stats
	    quietly sum `var'_`sub', detail
		
		local p5_`sub'  = r(p5)
		local p50_`sub' = r(p50)
		local p95_`sub' = r(p95)

	}
	
	* write one line
	if "`var'" != "num_industries" &  "`var'" != "tr_agg"  &  "`var'" != "leakagerate"{
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.1f (`p50_`sub'') "$"
		}
		file write Table 	" \\ " _n
		foreach sub in  $caselist {
			file write Table " &" _tab  "$[" %7.1f (`p5_`sub'') "," %7.1f (`p95_`sub'') "]$" 
		}
		file write Table " \\[0.5em]" _n
	}
	else if ("`var'" == "leakagerate") {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.2f (`p50_`sub'') "$"
		}
		file write Table 	" \\ " _n
		foreach sub in  $caselist {
			file write Table " &" _tab  "$[" %7.2f (`p5_`sub'') "," %7.2f (`p95_`sub'') "]$" 
		}
		file write Table " \\[0.5em]" _n
	}
	else if ( "`var'" == "tr_agg") {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.2f (`p50_`sub'') "$"
		}
		file write Table " \\[0.5em]" _n
	}
	else {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.0f (`p50_`sub'') "$"
		}
		file write Table " \\[0.5em]" _n
	}

}
*


* end of table
file write Table "\bottomrule " _n
file write Table "\end{tabular*} " _n

* footnote
*file write Table "\vspace{-1ex} " _n
*file write Table "\begin{minipage}{\textwidth} " _n
*file write Table "{\footnotesize \textit{Notes:} All values deflated to 2007 U.S. dollars.} "_n
*file write Table "\end{minipage} " _n


* close file
file close Table
